﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Coffee.SurveyControls.SurveyQuestions;
using System.Xml;
using System.Data;
using System.Web.UI.WebControls;

namespace Coffee
{
    class ValueConverterProviders
    {
        /// <summary>
        /// Allows web control size-related properties to be set
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object WebControlsUnitProvider(object value)
        {
            return System.Web.UI.WebControls.Unit.Parse((string)value);
        }

        public static object Int32Provider(object value)
        {
            return Int32.Parse((string)value);
        }

        public static object DateTimeProvider(object value)
        {
            return DateTime.Parse((string)value);
        }

    }

    class ValueParserProviders
    {

        public static object SessionProvider(string text)
        {
            return HttpContext.Current.Session[text];
        }

        public static object ResourceProvider(string text)
        {
            string classprop = text;
            string[] cp = classprop.Split(new char[] { '.' });
            return HttpContext.GetGlobalResourceObject(cp[0], cp[1]);
        }

        public static object CallMethodProvider(string text)
        {
            string className;
            string function;
            int dot = text.IndexOf('.');
            int oparen = text.IndexOf('(');
            int cparen = text.IndexOf(')');
            // throw a tantrum if any of the above are missing
            className = text.Substring(0, dot);
            function = text.Substring(dot + 1, oparen - dot - 1);
            List<String> parameters = Parser.ParseParameters(text.Substring(oparen + 1, cparen - oparen - 1));
            if (parameters.Count == 0)
            {
                return Reflection.CallMethod(className, function, null);
            }
            else
            {
                return Reflection.CallMethod(className, function, parameters.ToArray());
            }
        }

        public static object ControlProvider(string text)
        {
            string[] cp = text.Split(new char[] { '.' });
            object target = SurveyManager.FindControl(cp[0]);
            if (target == null)
            {
                throw new Exception(string.Format("Cound not find the control '{0}'", cp[0]));
            }
            if (typeof(SurveyQuestionRow).IsAssignableFrom(target.GetType()))
            {
                target = ((SurveyQuestionRow)target).CurrentControl;
            }
            return Reflection.GetProperty(cp[1], target);
        }

    }

    class BindingProviders 
    {
        public static void CallMethodProvider(DataBoundControl control, XmlNode node)
        {
            DataSet ds = (DataSet)Parser.ParseValue(node.SelectSingleNode("name"), "function");

            if (ds.Tables[0].Rows.Count > 0)
            {
                control.DataSource = ds.Tables[0];
                Binding.Map(control, node);
            }
        }

        public static void ListProvider(DataBoundControl control, XmlNode node)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Text");
            dt.Columns.Add("Value");

            XmlNodeList items = node.SelectNodes("item");
            foreach (XmlNode item in items)
            {
                dt.Rows.Add(new object[] {
                    (string)Parser.ParseValue(item.SelectSingleNode("text")),
                    (string)Parser.ParseValue(item.SelectSingleNode("value"))
                    });
            }

            if (dt.Rows.Count > 0)
            {
                control.DataSource = dt;
                Binding.Map(control, node);
            }

        }

        public static void SQLStoredProcProvider(DataBoundControl control, XmlNode node)
        {
            DataSet ds = Coffee.SQLClient.ExecStoredProc(node);

            if (ds.Tables[0].Rows.Count > 0)
            {
                control.DataSource = ds.Tables[0];
                Binding.Map(control, node);
            }

        }
    }
}
